home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $getclauses.P < prev    next >
Text File  |  1990-04-12  |  5KB  |  121 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $getclauses.P */
  25.  
  26. $getclauses_export([$getclauses/2,$getclauses/3,$attach/2,$expand_term/2]).
  27.  
  28. $getclauses_use($bio,[_,_,_,_,_,_,_,_,_,_,$see/1,$seeing/1,$seen/0]).
  29. $getclauses_use($listutil1,[$reverse/2,$merge/3,$absmember/2,$absmerge/3,
  30.         $nthmember/3,$nthmember1/3,$member2/2,$closetail/1]).
  31. $getclauses_use($blist,[$append/3,$member/2,$member1/2]).
  32. $getclauses_use($meta,[$functor/3,$univ/2,$length/2]).
  33. $getclauses_use($read,[$read/1,$read/2]).
  34.  
  35. $getclauses(Filename,ClauseList) :- $getclauses(Filename,ClauseList,_).
  36.  
  37. $getclauses(Filename, ClauseList,PredList) :-
  38.     $seeing(InStr),
  39.     $see(Filename),
  40.     $getclauses1(ClauseList0,[],PredList),
  41.     $seen,
  42.     $see(InStr),
  43.     $getcl_collect(ClauseList0, ClauseList),
  44.     $getcl_closetails(ClauseList).
  45.  
  46. $getclauses1(ClauseList,ClauseListTail,PredList) :-
  47.     $read(T),
  48.     $expand_term(T,T0),
  49.     $fun_rel(T0,Cl),
  50.     (Cl = end_of_file ->
  51.         (ClauseListTail = ClauseList,
  52.          $closetail(PredList),
  53.      !
  54.         ) ;
  55.     (Cl = ':-'(Command) ->
  56.         (call(Command),
  57.          $getclauses1(ClauseList,ClauseListTail,PredList)
  58.         ) ;
  59.              (ClauseList = [Cl | ClauseListRest],
  60.               $getcl_pred(Cl,P,N),
  61.           $member1( (P/N), PredList),
  62.                 $getclauses1(ClauseListRest,ClauseListTail,PredList)
  63.         )
  64.     )
  65.     ).
  66.  
  67. /*  "$getcl_collect" is used to gather together the clauses for individual
  68.      predicates.  Each entry for a predicate is of the form
  69.  
  70.        pred(Pred,Arity,CpyFlag,CutFlag,ClauseList).
  71.  
  72.      Once all the clauses have been read, the list of such 5-tuples
  73.      that has been constructed will be traversed to set the values
  74.      of "CpyFlag" and "CutFlag", which are as follows: if "CpyFlag"
  75.      is 1 then the predicate contains constructs like cut, negation or
  76.      if-then-else that require copying, while if it is 0 then it does
  77.      not; "CutFlag" indicates whether or not the predicate contains cuts.
  78.      This is useful because whenever cuts or negations are present, the
  79.      program must be transformed to handle them, and this involves the
  80.      creation of structures on the heap.
  81.      
  82.      Each clause is represented as terms of the form
  83.      
  84.          fact(Fact,CpyFlagRest) or rule(Head,Body,CpyFlag,CpyFlagRest)
  85.     
  86.     where CpyFlag is 1 or 0 depending on whether the rule contains
  87.     constructs like cut, negation or if-then-else, and CpyFlagRest gives
  88.     the same information for the remaining clauses.              */
  89.  
  90. $getcl_collect([],L) :- $closetail(L), !.
  91. $getcl_collect([(H :- B)|PRest],L) :- !,
  92.     $functor(H,Pred,Arity),
  93.     $member1(pred(Pred,Arity,_,_,Clauses),L),
  94.     $attach(rule(H,B,_,_),Clauses),
  95.     $getcl_collect(PRest,L).
  96. $getcl_collect([(Fact)|PRest],L) :-
  97.     $functor(Fact,Pred,Arity),
  98.     $member1(pred(Pred,Arity,_,_,Clauses),L),
  99.     $attach(fact(Fact,_),Clauses),
  100.     $getcl_collect(PRest,L).
  101.  
  102. $attach(X,Y) :- (var(Y), Y = [X|_]) ;
  103.            (nonvar(Y), Y = [_|T], $attach(X,T)).
  104.  
  105.  
  106. $getcl_pred((H :- B),P,N) :- !, $functor(H,P,N).
  107. $getcl_pred(Fact,P,N) :- $functor(Fact,P,N).
  108.  
  109. $getcl_closetails([]).
  110. $getcl_closetails([Pred|PRest]) :-
  111.     $getcl_closetails1(Pred), !, $getcl_closetails(PRest).
  112.  
  113. $getcl_closetails1(pred(P,N,_,_,Clauses)) :- $closetail(Clauses).
  114.  
  115. $expand_term(T1,T2) :- term_expansion(T1,T2), !.
  116. $expand_term(T1,T2) :- functor(T1,'-->',2), !, $dcg(T1,T2).
  117. $expand_term(T,T).
  118.  
  119. /* --------------------------- $getclauses.P --------------------------- */
  120.  
  121.